<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Http\Request;

class userlogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        if (!$request->session()->has('user')) {
            return redirect('/user/login')->with('danger', '请先登录');;
        }
        $user=User::where('id',session('user')->id)->first();
        if ($user->status==0) {
            session(['user'=>null]);
            return redirect('/login')->with('danger', '当前账号以被封禁，请联系管理员解封');;
        }

        return $next($request);
    }
}
